package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.ArtDynamicAdapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.DynamicList;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

/**
 * 
 * 
 * 项目名称：ArtworkArtA 类名称：ArtDynamicActivity 类描述：艺术家动态 创建人：耿卫斌 创建时间：2016年5月16日
 * 下午1:22:13
 * 
 */
public class ArtDynamicActivity extends BaseActivity implements OnClickListener {
	private ArtDynamicAdapter mAdapter;
	private Context mContext;
	private String release_status;
	private TextView info;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private List<DynamicList> list;
	private List<DynamicList> more_list;
	View hint;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dynamic_artist);
		setContent(this, getIntent());
		setBack();
		mContext = this;
		initview();
	}

	private void initview() {
		// 注册当前广播
		IntentFilter filter = new IntentFilter("com.ArtDynamicActivity.receiver");
		mContext.registerReceiver(broadcastReceiver, filter);
		list = new ArrayList<DynamicList>();
		more_list = new ArrayList<DynamicList>();
		hint = (View) findViewById(R.id.dyn_blank_hint);
		mTittle = (TextView) findViewById(R.id.title_text);
		setBack();
		mTittle.setText("艺术品动态");
		mFbText = (TextView) findViewById(R.id.title_submit);
		mFbText.setPadding(30, 15, 30, 15);
		mFbText.setText("发布");
		info = (TextView) findViewById(R.id.tv_info);
		mBack.setOnClickListener(this);
		mFbText.setVisibility(View.VISIBLE);
		mFbText.setOnClickListener(this);
		/**
		 * 判断是否认证，未认证不可申请备案
		 */
		if ("2".equals(AppUtil.getUser().getIs_auth())
				|| "0".equals(AppUtil.getUser().getIs_auth())
				|| "-1".equals(AppUtil.getUser().getIs_auth())) {
			info.setVisibility(View.VISIBLE);
			mFbText.setVisibility(View.GONE);
		}
		mListView = (PullableListView) findViewById(R.id.dynamic_listivew);
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.dynamic_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				page = 1;
				isRefreshed = true;
				reqArtDynamicListData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				page++;
				reqArtDynamicListData();
			}
		});
		reqArtDynamicListData();// 动态列表
	}

	private void reqArtDynamicListData() {
		VolleyBasic.getInstance(ArtDynamicActivity.this)
				.post(Cache.artworklist,
						VolleyMap.getInstance().artworkList(
								String.valueOf(page), "10"), DynamicList.class,
						new VolleyCallback() {

							@SuppressWarnings("unchecked")
							@Override
							public void onSuccess(VolleyData vd) {
								// TODO Auto-generated method stub
								mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
								list = (List<DynamicList>) vd.getList();
								if (isRefreshed == true) {
									more_list.clear();
									isRefreshed = false;
								}
								if (!list.isEmpty()) {
									more_list.addAll(list);
								}
								if (more_list == null | more_list.size() == 0) {
									// 没数据的时候
									hint.setVisibility(View.VISIBLE);
									return;
								}
								hint.setVisibility(View.GONE);
								if (mAdapter == null) {
									mAdapter = new ArtDynamicAdapter(mContext);
									mListView.setAdapter(mAdapter);
								}
								mAdapter.setdata(more_list);
								mAdapter.notifyDataSetChanged();
								mListView
										.setOnItemClickListener(new OnItemClickListener() {

											@Override
											public void onItemClick(
													AdapterView<?> parent,
													View view, int position,
													long id) {
												Intent intent = new Intent(
														ArtDynamicActivity.this,
														ArtDynamicDetailActivity.class);
												intent.putExtra("id", more_list
														.get(position)
														.getArt_id());
												intent.putExtra("type", more_list
														.get(position)
														.getType());
												startActivity(intent);
											}
										});
							}

							@Override
							public void onError(VolleyError volleyError) {
								mPullLayout.loadmoreFinish(mPullLayout.FAIL);
							}
						});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_submit:
			Intent in = new Intent(mContext, ReleaseDynamicActivity.class);
			startActivity(in);
			break;

		default:
			break;
		}

	}

	/**
	 * 广播接收器
	 */
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getBooleanExtra("isRefresh", false) == true) {
				Log.d("log", "通知艺术家动态列表刷新");
				page = 1;
				isRefreshed = true;
				mAdapter = null;
				reqArtDynamicListData();
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		// 销毁广播
		mContext.unregisterReceiver(broadcastReceiver);
	};

}
